package com.gage.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	
	private static String defaultDatePattern = "yyyy-MM-dd";  
	private static String defaultDateTimePattern = "yyyy-MM-dd HH:mm:ss"; 

	/**
	 * 将date转化为String yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String DateToString(Date date){
		String sdate;  

		sdate=(new SimpleDateFormat(defaultDatePattern)).format(date);          
		return sdate;
	}
	
	/**
	 * 将date转化为时间与日期 yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String DateToDateTimeString(Date date){
		String sdate;  

		sdate=(new SimpleDateFormat(defaultDateTimePattern)).format(date);          
		return sdate;
	}
	/**
	* 字符串转换成日期
	* @author lkr
	* @param str
	* @return date
	*/
	public static Date StrToDate(String str) {
	  
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	   Date date = null;
	   try {
	    date = format.parse(str);
	   } catch (ParseException e) {
	    e.printStackTrace();
	   }
	   return date;
	}

	/**
	 * 字符串转换成日期
	 * @author lkr
	 * @param str
	 * @return date
	 */
	public static Date StrToDatetime(String str) {
		
		SimpleDateFormat format = new SimpleDateFormat(defaultDateTimePattern);
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 查询时间段时获取后一天日期
	 * @author lkr
	 * @param d
	 * @return
	 */
	public static Date getDateLastDay(Date d) {
		Calendar   calendar   =   new   GregorianCalendar(); 
		calendar.setTime(d); 
		calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
		Date date1=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		return date1;
	}

	
	/**
	 * 时间类型加减天数
	 * @param date 时间
	 * @param diff 加减的天数
	 * @return
	 */
	public static Date dateAddOrMinus(Date date,int diff){
		Date dateafter = null;
		if(date != null){
			Calendar calendar=Calendar.getInstance();   
			calendar.setTime(date); 
			calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+diff);
			dateafter = calendar.getTime();
		}
	    return dateafter;
	}


}
